//
//  FZNetworking.swift
//  FZSina_Swift
//
//  Created by 李忠 on 7/21/16.
//  Copyright © 2016 FZLZ. All rights reserved.
//

import UIKit
import AFNetworking

enum FZNetworkingType: String {
    case GET = "GET"
    case POST = "POST"
}

class FZNetworking: AFHTTPSessionManager{
    
    /// MARK: 单例
    static let sharedTools: FZNetworking = {
        
        let tools: FZNetworking = FZNetworking()
        
        //可以使用数组一次吧字节加上去
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    
    //定义请求
    typealias FZRequestCallBack = (responseObject: AnyObject?, error: NSError?)->()
    
    func required(methed: FZNetworkingType = .GET, urlString: String, parameters:AnyObject?, finised: FZRequestCallBack){
        //定义一个请求成功后要执行的闭包
        let success = {
            (task :NSURLSessionDataTask, responseObject: AnyObject?)->() in
            finised(responseObject: responseObject, error: nil)
        }
        //定义失败后执行的方法
        let failure = {
            (task :NSURLSessionDataTask?, error: NSError)->() in
            finised(responseObject: nil, error: error)
        }
        
        //开始网络请求
        if methed == .GET {
            GET(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }else{
            POST(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
        
    }
}

//MARK:==========================授权请求==========================
extension FZNetworking{
    func oauthLoadUserAccount(code: String, finished: FZRequestCallBack){
        
        //请求参数
        let urlString: String = "https://api.weibo.com/oauth2/access_token"
        let parameters: [String: AnyObject] = [
            "client_id" : APPKEY,
            "client_secret" : APPSECRET,
            "grant_type" : "authorization_code",
            "code" : code,
            "redirect_uri" : APPREDIRECT_URI
        ]
        //开始请求
        required(.POST, urlString: urlString, parameters: parameters, finised: finished)
    }
    
    func oauthLoadUserAcccountInfo(userAcount: FZUserAccountModel, finished: FZRequestCallBack) {
        
        
        let urlString: String = "https://api.weibo.com/2/users/show.json"
        /*
         access_token   采用OAuth授权方式为必填参数，OAuth授权后获得。
         uid            需要查询的用户ID。
         screen_name	需要查询的用户昵称。
         */
        let parameters: [String: AnyObject] = [
            "access_token": userAcount.access_token!,
            "uid": userAcount.uid!
        ]
        required(urlString: urlString, parameters: parameters, finised: finished)
        
    }
}

//MARK:==========================首页==========================
extension FZNetworking{
    func homeLoadStatusData(since_id: Int64 = 0,max_id: Int64 = 0, finished: FZRequestCallBack) {
        let urlString: String = "https://api.weibo.com/2/statuses/home_timeline.json"
        ///access_token	true	string	采用OAuth授权方式为必填参数，OAuth授权后获得。
        ///since_id	若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。
        //max_id	若指定此参数，则返回ID小于或等于max_id的微博，默认为0。
        
        let parameters: [String: AnyObject] = [
            "access_token": FZUserAccountViewModel.sharedUserAcount.Access_Token!,
            "since_id" : "\(since_id)",
            "max_id" : "\(max_id)"
        ]
        
        required(urlString: urlString, parameters: parameters, finised: finished)
    }
}
